package com.frank.loadinglibrary.common.element;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

/**
 * @author SUQI
 * @date 2021/6/3
 * @description
 **/
public class WifiLoadingElement extends BaseElement implements BaseAnimationController.IAnimationController<AnimatorValue> {

    private static final int VALUETHREE = 3;
    private int num = 3;
    private float baseRadius;
    private Paint mPaint;
    private float currentValue;
    private Color[] colors;
    private int duration;

    private WifiLoadingElement(IComponentCallback iComponentCallback) {
        this.callback = iComponentCallback;
    }

    @Override
    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> list = new ArrayList<>();
        AnimatorValue animatorValue = new AnimatorValue();
        animatorValue.setLoopedCount(Animator.INFINITE);
        animatorValue.setDuration(duration);
        animatorValue.setValueUpdateListener((animatorValue1, v) -> {
            currentValue = v;
            callback.reflushComponent();
        });
        list.add(animatorValue);
        return list;
    }

    @Override
    public void onAnimationReset() {
        //DO nothing because donot need to reset Animation params
    }

    @Override
    public BaseAnimationController.IAnimationController initAnimationController() {
        return this;
    }

    @Override
    public void initPaint() {
        super.initPaint();
        mPaint = new Paint();
        mPaint.setColor(Color.MAGENTA);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.save();
        canvas.translate(0, baseRadius);
        float broad = Math.min(getWidth(), getHeight());
        baseRadius = broad / 2f / num;
        float strokeWidth = baseRadius / 2f;
        for (int i = 0; i < num; i++) {
            mPaint.setColor(colors[i]);
            float radius = baseRadius * i;
            RectFloat rect = new RectFloat(radius + strokeWidth, radius + strokeWidth, broad - radius - strokeWidth, broad - radius - strokeWidth);
            HiLog.debug(new HiLogLabel(HiLog.DEBUG, 1, "sss"), "index: " + currentValue * (num));
            if (i >= num - (int) (currentValue * (num + 1))) {
                if (i == num - 1) {
                    mPaint.setStyle(Paint.Style.FILL_STYLE);
                    mPaint.setStrokeWidth(0);
                    canvas.drawArc(rect, new Arc(0, 360, true), mPaint);
                } else {
                    mPaint.setStyle(Paint.Style.STROKE_STYLE);
                    mPaint.setStrokeWidth(strokeWidth);
                    canvas.drawArc(rect, new Arc(-90, 90, false), mPaint);
                }
            }
        }
        canvas.restore();
    }

    public void apply(WifiBuilder builder) {
        if (builder.colors == null || builder.colors.length != VALUETHREE) {
            this.colors = new Color[]{new Color(Color.getIntColor("#abbd81")), new Color(Color.getIntColor("#f8b26a")), new Color(Color.getIntColor("#f47e60"))};
        } else {
            this.colors = builder.colors;
        }
        this.duration = builder.duration > 0 ? builder.duration : 1000;
    }

    public static class WifiBuilder {
        private IComponentCallback iComponentCallback;
        private Color[] colors;
        private int duration;

        public WifiBuilder(IComponentCallback iComponentCallback) {
            this.iComponentCallback = iComponentCallback;
        }

        public WifiBuilder setColors(Color[] colors) {
            this.colors = colors;
            return this;
        }

        public WifiBuilder setDuration(int duration) {
            this.duration = duration;
            return this;
        }

        public WifiLoadingElement build() {
            WifiLoadingElement wifiLoadingElement = new WifiLoadingElement(iComponentCallback);
            wifiLoadingElement.apply(this);
            return wifiLoadingElement;
        }
    }
}
